package service

import (
	"context"
	"errors"
	__ "user_srv/basic/userproto"
	"user_srv/handler/dao"
)

// 人脸信息注册
func (s *Server) RegisterFace(_ context.Context, in *__.RegisterFaceReq) (*__.RegisterFaceResp, error) {
	id, err := dao.AddFaceWithUserId(in.UserId, in.Img)
	if err != nil {
		return nil, errors.New("添加人脸失败:" + err.Error())
	}
	return &__.RegisterFaceResp{Id: id}, nil
}

// 人脸对比
func (s *Server) MatchFaceInfo(_ context.Context, in *__.MatchFaceInfoReq) (*__.MatchFaceInfoResp, error) {
	res, err := dao.MatchLogic(in.UserId, in.Img)
	if err != nil {
		return nil, errors.New("人脸对比失败:" + err.Error())
	}
	return &__.MatchFaceInfoResp{
		UserId: res,
	}, nil
}

// 人脸更新
func (s *Server) UpdateFaceInfo(_ context.Context, in *__.UpdateFaceInfoReq) (*__.UpdateFaceInfoResp, error) {
	res := dao.UpdateFace(in.UserId, in.Img)
	if res != nil {
		return nil, errors.New("更新人脸失败:" + res.Error())
	}
	return &__.UpdateFaceInfoResp{Res: true}, nil
}

// 删除人脸数据
func (s *Server) DeleteFaceInfo(_ context.Context, in *__.DeleteFaceInfoReq) (*__.DeleteFaceInfoResp, error) {
	res := dao.DeleteFace(in.UserId)
	if res != nil {
		return nil, errors.New("删除失败" + res.Error())
	}
	return &__.DeleteFaceInfoResp{Res: true}, nil
}
